<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Course
 *
 * @property-read \App\Models\Category $category
 * @method static bool|null forceDelete()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Course newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Course newQuery()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Course onlyTrashed()
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Course query()
 * @method static bool|null restore()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Course withTrashed()
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Course withoutTrashed()
 * @mixin \Eloquent
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Classes[] $classes
 * @property-read string $cover
 */
class Course extends Base
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $fillable = ['name','teacher','category_id','intro','cover'];

    /**
     * 分类信息
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    /**
     * 课程下所有课时
     */
    public function classes()
    {
        return $this->hasMany(Classes::class);
    }

    /**
     * 给封面加上域名
     * @param $val
     * @return string
     */
    public function getCoverAttribute($val)
    {
        if ($val) return config('app.url') . $val;
        return $val;
    }
}
